<template>
  <div class="intro">
    <p class="detail-tip">
      企业简介<span v-if="showView" @click="toggleView">编辑</span>
    </p>
    <div class="com-container" v-if="showView">
      <div class="line">
        <p class="pie" v-html="intro"></p>
      </div>
    </div>
    <div class="com-form" v-else>
      <div class="postForm">
        <div class="form-item">
          <div class="text" required>企业简介:</div>
          <div class="textarea">
            <textarea
              v-model="intro"
              maxlength="700"
              placeholder="请输入企业简介"
            />
            <p class="count">
              你还可以输入<span>{{ textCount }}</span
              >字
            </p>
          </div>
        </div>
        <div class="submit">
          <button class="confirm" type="button" @click="submit">保存</button>
          <button class="cancel" type="button" @click="cancel">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ['description'],
  data () {
    return {
      intro: '',
      showView: true,
      textCount: 700
    }
  },
  watch: {
    intro (val) {
      this.textCount = 700 - val.length;
    },
    description(val) {
      this.intro = val;
      this.toCheck();
    }
  },
  created () {
    this.intro = this.description;
    this.toCheck();
  },
  mounted () {
  },
  methods: {
    toggleView () {
      this.showView = false;
    },
    toCheck () {
      this.intro = this.intro.replace(/\n/g, '<br/>');
    },
    cancel () {
      this.showView = true;
    },
    submit () {
      this.showView = true;
      this.$emit('submit_desc', this.intro);
    },
  }
}
</script>
<style lang="scss" scoped>
.intro {
  position: relative;
  .line {
    padding-left: 25px;
    line-height: 25px;
    margin-bottom: 15px;
    .pie {
      display: inline-block;
      position: relative;
      padding-left: 8px;
      margin-right: 20px;
      font-size: 16px;
      color: #999;
    }
  }
  .com-form {
    padding: 30px 0;
    .postForm {
      padding-left: 350px;
      .form-item {
        margin-bottom: 10px;
        line-height: 40px;
        & > div {
          display: inline-block;
        }
        & > .text {
          position: relative;
          padding-left: 20px;
          min-width: 120px;
          color: #24292f;
          vertical-align: top;
          &[required] {
            &::before {
              content: '*';
              position: absolute;
              left: 0px;
              top: 0px;
              color: $base-color-danger;
            }
          }
        }

        .textarea {
          margin-top: 10px;
          textarea {
            resize: none;
            height: 140px;
            width: 360px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 10px;
            color: #24292f;
            font-size: 16px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
          .count {
            text-align: right;
            span {
              color: $base-color;
              margin: 0 5px;
            }
          }
        }
      }
      .submit {
        margin-top: 60px;
        padding-left: 30px;
        text-align: left;
        button {
          height: 45px;
          width: 150px;
          margin: 0 20px;
          border: 1px solid $base-color;
          border-radius: 6px;
          font-size: 16px;
          &:hover {
            box-shadow: 0 0 6px 0 $base-color;
          }
        }
        .cancel {
          background: #fff;
          color: $base-color;
        }
        .confirm {
          background: $base-color;
          color: #fff;
        }
      }
    }
  }
}
</style>
